 
import { EBoostState } from "GameLoop/WorldState/mount/creep/extension";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { BoostState } from "GameLoop/WorldState/EntityManager/Entitys/room/lab/types";
import { CreepBoostSetup } from "utils/creep/CreepBoostSetup";
import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { ATeamEntity } from "GameLoop/WorldState/EntityManager/Entitys/team";
 
/**
 * 爬boost状态时 
 * 如果是单个爬就直接进行boost
 * 如果是team的爬要等待team的成员集齐后。各自再进行boost
 */
export class CreepBoostState<T extends IStateArg<Creep>>  extends State<T>
{
   
    public getExitActions(arg: T): Action[] 
    {
        const creep = arg.entity;
        if(creep.memory.spawn)
        {
            const room = creep.ownerRoom();
            room.getEntity("SpawnEntity").releaseRenewSlot(creep);
        }
        return []
    }
    //
    public  getActions(arg:T):Action[]
    {
        const creep = arg.entity;
        if(!creep.memory.taskid && !creep.memory.teamid)
        {
            return []
        }
        let taskid = creep.memory.taskid;
        if(creep.memory.teamid)
        {
            const team = arg.entity.getEntityByID(creep.memory.teamid);
            if(!team)
            {
                creep.log('info',"错误，成员找不到小队");
                return [];
            }
            else
            {
                if(!(team as ATeamEntity).isTeamMoveReady)
                {
                    // 小队成员还没有准备好
                    creep.log('info',"小队队员还没有就绪")
                    return [];
                }
                 taskid = (team.getNodeMemory() as any).bindTask;
            }
        }
        if(!taskid)
        {
            return []
        }
        if(creep.isBoostComplete())
        {
            return []
        }
        const task =  creep.ownerRoom().findTaskById<ITaskDeclare["team"]>(taskid,"team");
        if(!task)
        {
            return []
        }
        
        const thisroom = creep.ownerRoom();
        let boostTask = thisroom.findTaskById<ITaskDeclare["boost"]>(creep.name,"boost");
        if(!boostTask)
        {
            const bodyconfig = CreepBoostSetup.BoostCreepConfig(creep,creep.memory.BoostLevel as BoostLevel);
            global.Log.Info(`${creep.name}  强化配置 ${JSON.stringify(bodyconfig)}`)
            // 投递强化任务
            const tmpboostTask:IBoostTask={
                res:bodyconfig,
                State:BoostState.init
            }
            const btask :ITaskDeclare["boost"]=
            {
                id:creep.name,
                ttl:5000,
                priority:task.priority,
                type:"boost",
                data:{
                    type:"boost",
                    arg:tmpboostTask
                },
                shard:1,
            }
            thisroom.addNewTask(btask);
            boostTask  = btask
        }
        const targetroom =  Game.rooms[task.data.room];
        const isBoostReady=()=>
        {
            if(!targetroom || !targetroom.IsMy() || targetroom.memory.state != 2)
            {
                 
                // 这些情况lab准备好了就直接去boost
                return boostTask?.data.arg.State== BoostState.ready
            }
            return false
 
        }
        if(creep.body.find(e=>e.boost))
        {
           return  [ActionArgHelper.actionCreepBoost(creep,creep.ownerRoom(),boostTask.data.arg)]
        }
        switch( creep.memory.boostState)
        {
            case EBoostState.Renew:
                return this.onRenewActions(creep,isBoostReady)    
            ;
            case EBoostState.Boost:
                return this.onBoostActions(creep,boostTask.data.arg,isBoostReady)    
            ;
            default:
                creep.memory.boostState = EBoostState.Renew;
        }
        return [ ];
    }

    
     
    private onRenewActions(creep:Creep,isBoostReady:()=>boolean)
    {
        // ttl>1400
        //         hasenemy => boost
        //          noenemy => watch
        const room = creep.ownerRoom();
        const spawn =  room.getEntity("SpawnEntity");
 
        if((creep.ticksToLive as number)>1400 || spawn.spawnQueue.size()>0)
        {   
            // 孵化任务堆积，则直接boost
            // 当已经有部分bodyboost之后。就不renew了
            if(isBoostReady())
            {
                creep.memory.boostState = EBoostState.Boost;
            }
            else
            {

            }
            if(creep.memory.spawn)
            {
               
                room.getEntity("SpawnEntity").releaseRenewSlot(creep);
            }
        }
        else
        {
            const room = creep.ownerRoom();
         

            // 执行renew
            return [ActionArgHelper.actionCreepRenew(creep,creep.ownerRoom())]
        }
        return []
    }

    private onBoostActions(creep:Creep,boostTask:IBoostTask,isBoostReady:()=>boolean)
    {
        // ttl<1350 
        //     no boost =>renew 
        const room = creep.ownerRoom();
        const spawn =  room.getEntity("SpawnEntity");
        if(isBoostReady())
        {
            
            // 执行boost
            if((creep.ticksToLive as number)<1200 && spawn.spawnQueue.size() == 0)
            {
                creep.memory.boostState = EBoostState.Renew;
            }
            else
            {
               
                // 执行boost任务
                return [ActionArgHelper.actionCreepBoost(creep,creep.ownerRoom(),boostTask)]
            }
        }
        else
        {
            if((creep.ticksToLive as number)<1350)
            {
                creep.memory.boostState = EBoostState.Renew;
            }
        }
        return []
    }


    
}
